package com.ruoyi.know.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.know.domain.CommunitySections;
import com.ruoyi.know.service.ICommunitySectionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 社区板块Controller
 *
 * @author ruoyi
 * @date 2025-02-10
 */
@RestController
@RequestMapping("/know/sections")
public class CommunitySectionsController extends BaseController
{
    @Autowired
    private ICommunitySectionsService communitySectionsService;

    /**
     * 查询社区板块列表
     */
    @PreAuthorize("@ss.hasPermi('know:sections:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommunitySections communitySections)
    {
        startPage();
        List<CommunitySections> list = communitySectionsService.selectCommunitySectionsList(communitySections);
        return getDataTable(list);
    }

    /**
     * 导出社区板块列表
     */
    @PreAuthorize("@ss.hasPermi('know:sections:export')")
    @Log(title = "社区板块", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommunitySections communitySections)
    {
        List<CommunitySections> list = communitySectionsService.selectCommunitySectionsList(communitySections);
        ExcelUtil<CommunitySections> util = new ExcelUtil<CommunitySections>(CommunitySections.class);
        util.exportExcel(response, list, "社区板块数据");
    }

    /**
     * 获取社区板块详细信息
     */
    @PreAuthorize("@ss.hasPermi('know:sections:query')")
    @GetMapping(value = "/{sectionId}")
    public AjaxResult getInfo(@PathVariable("sectionId") Long sectionId)
    {
        return success(communitySectionsService.selectCommunitySectionsBySectionId(sectionId));
    }

    /**
     * 新增社区板块
     */
    @PreAuthorize("@ss.hasPermi('know:sections:add')")
    @Log(title = "社区板块", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommunitySections communitySections)
    {
        communitySections.setCreatedBy(getUsername());
        communitySections.setCreatedAt(new Date());
        return toAjax(communitySectionsService.insertCommunitySections(communitySections));
    }

    /**
     * 修改社区板块
     */
    @PreAuthorize("@ss.hasPermi('know:sections:edit')")
    @Log(title = "社区板块", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommunitySections communitySections)
    {
        communitySections.setUpdateBy(getUsername());
        communitySections.setUpdateTime(new Date());
        return toAjax(communitySectionsService.updateCommunitySections(communitySections));
    }

    /**
     * 删除社区板块
     */
    @PreAuthorize("@ss.hasPermi('know:sections:remove')")
    @Log(title = "社区板块", businessType = BusinessType.DELETE)
	@DeleteMapping("/{sectionIds}")
    public AjaxResult remove(@PathVariable Long[] sectionIds)
    {
        return toAjax(communitySectionsService.deleteCommunitySectionsBySectionIds(sectionIds));
    }
}
